using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Universal.Domain.Entities.App;

namespace Universal.Infrastructure.DB.Config;

public class AppUserConfiguration : IEntityTypeConfiguration<AppUser>
{
    public void Configure(EntityTypeBuilder<AppUser> builder)
    {
        // 多对多：user_roles
        builder
            .HasMany(u => u.Roles)
            .WithMany(r => r.Users)
            .UsingEntity<Dictionary<string, object>>(
                "user_roles",
                j => j.HasOne<AppRole>().WithMany().HasForeignKey("role_id"),
                j => j.HasOne<AppUser>().WithMany().HasForeignKey("user_id"),
                j =>
                {
                    j.HasKey("user_id", "role_id");
                    j.ToTable("user_roles");
                });

        // 一对多：user-files
        builder
            .HasMany(u => u.Files)
            .WithOne(f => f.User)
            .HasForeignKey(f => f.UserId);

        // 一对多：user-articles
        builder
            .HasMany(u => u.Articles)
            .WithOne(a => a.User)
            .HasForeignKey(a => a.UserId);
    }
}
